home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Magazine / Online / OpenURL / Developer / Source / prefs_main.c < prev    next >
C/C++ Source or Header  |  1999-09-26  |  7KB  |  259 lines

  1. /*
  2. ** OpenURL - MUI preferences for openurl.library
  3. ** Written by Troels Walsted Hansen <troels@thule.no>
  4. ** Placed in the public domain.
  5. **
  6. ** This module contains all initialization code and the main function.
  7. */
  8.  
  9. #include "prefs_common.h"
  10. #include "prefs_main.h"
  11. #include "prefs_app.h"
  12. #include "prefs_prefswin.h"
  13. #include "prefs_applist.h"
  14. #include "prefs_browsereditwin.h"
  15. #include "prefs_mailereditwin.h"
  16.  
  17. /**************************************************************************/
  18.  
  19. /* prototypes */
  20.  
  21. static STRPTR OpenLibs(VOID);
  22. static VOID   CloseLibs(VOID);
  23. static VOID   InitClassnames(VOID);
  24. static STRPTR CreateClasses(VOID);
  25. static VOID   DestroyClasses(VOID);
  26. static struct DiskObject *GetMyDiskObject(int argc, char **argv);
  27.  
  28. /**************************************************************************/
  29.  
  30. /* stack */
  31.  
  32. LONG __stack = 8192;
  33.  
  34. /* library bases */
  35.  
  36. struct IntuitionBase *IntuitionBase = NULL;
  37. struct Library *MUIMasterBase = NULL, *UtilityBase = NULL, *IconBase = NULL, 
  38.                *OpenURLBase = NULL;
  39.  
  40. /* mui classes */
  41.  
  42. struct MUI_CustomClass *AppClass = NULL, *PrefsWinClass = NULL, *AppListClass = NULL, 
  43.                        *BrowserEditWinClass = NULL, *MailerEditWinClass = NULL;
  44.  
  45. /* mui class names */
  46.  
  47. static const STRPTR UsedClasses[] = { "BetterString.mcc", "Textinput.mcc", "NListviews.mcc", NULL };
  48. STRPTR stringclassname = "String.mui", listviewclassname = "Listview.mui", listclassname = "List.mui";
  49.  
  50. /**************************************************************************/
  51.  
  52. static STRPTR OpenLibs(VOID)
  53. {
  54.     if(!(MUIMasterBase = OpenLibrary("muimaster.library", 0)))
  55.         return("Requires muimaster.library V19+ (MUI 3.8+)");
  56.  
  57.     if(MUIMasterBase->lib_Version < 19)
  58.         return("Requires muimaster.library V19+ (MUI 3.8+)");
  59.  
  60.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37)))
  61.         return("Requires intuition.library V37+");
  62.  
  63.     if(!(UtilityBase = OpenLibrary("utility.library", 37)))
  64.         return("Requires utility.library V37+");
  65.  
  66.     if(!(IconBase = OpenLibrary("icon.library", 37)))
  67.         return("Requires icon.library V37+");
  68.  
  69.     if(!(OpenURLBase = OpenLibrary("openurl.library", 2)))
  70.         return("Requires openurl.library V2+");
  71.  
  72.     return(NULL);
  73. }
  74.  
  75. /**************************************************************************/
  76.  
  77. static VOID CloseLibs(VOID)
  78. {
  79.     if(OpenURLBase)   CloseLibrary(OpenURLBase);
  80.     if(IconBase)      CloseLibrary(IconBase);
  81.     if(UtilityBase)   CloseLibrary(UtilityBase);
  82.     if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  83.     if(MUIMasterBase) CloseLibrary(MUIMasterBase);
  84. }
  85.  
  86. /**************************************************************************/
  87.  
  88. static VOID InitClassnames(VOID)
  89. {
  90.     Object *o;
  91.  
  92.     /* string class */
  93.  
  94.     if((o = MUI_NewObjectA("Textinput.mcc", NULL)))
  95.         stringclassname = "Textinput.mcc";
  96.     else if((o = MUI_NewObjectA("BetterString.mcc", NULL)))
  97.         stringclassname = "BetterString.mcc";
  98.     else
  99.         stringclassname = "String.mui";
  100.  
  101.     if(o) MUI_DisposeObject(o);
  102.  
  103.     /* list and listview class */
  104.  
  105.     if((o = NListviewObject, MUIA_NListview_NList, NListObject, End, End))
  106.     {
  107.         listviewclassname = "NListview.mcc";
  108.         listclassname     = "NList.mcc";
  109.         MUI_DisposeObject(o);
  110.     }
  111. }
  112.  
  113. /**************************************************************************/
  114.  
  115. static STRPTR CreateClasses(VOID)
  116. {
  117.     if(!(AppClass = MUI_CreateCustomClass(NULL, MUIC_Application, NULL,
  118.        sizeof(struct App_Data), App_Dispatcher)))
  119.         return("Unable to create AppClass");
  120.  
  121.     if(!(PrefsWinClass = MUI_CreateCustomClass(NULL, MUIC_Window, NULL,
  122.        sizeof(struct PrefsWin_Data), PrefsWin_Dispatcher)))
  123.         return("Unable to create PrefsWinClass");
  124.  
  125.     if(!(AppListClass = MUI_CreateCustomClass(NULL, MUIC_Group, NULL,
  126.        sizeof(struct AppList_Data), AppList_Dispatcher)))
  127.         return("Unable to create AppListClass");
  128.  
  129.     if(!(BrowserEditWinClass = MUI_CreateCustomClass(NULL, MUIC_Window, NULL,
  130.        sizeof(struct BrowserEditWin_Data), BrowserEditWin_Dispatcher)))
  131.         return("Unable to create BrowserEditWinClass");
  132.  
  133.     if(!(MailerEditWinClass = MUI_CreateCustomClass(NULL, MUIC_Window, NULL,
  134.        sizeof(struct MailerEditWin_Data), MailerEditWin_Dispatcher)))
  135.         return("Unable to create MailerEditWinClass");
  136.  
  137.     return(NULL);
  138. }
  139.  
  140. /**************************************************************************/
  141.  
  142. static VOID DestroyClasses(VOID)
  143. {
  144.     if(MailerEditWinClass)  MUI_DeleteCustomClass(MailerEditWinClass);
  145.     if(BrowserEditWinClass) MUI_DeleteCustomClass(BrowserEditWinClass);
  146.     if(AppListClass)        MUI_DeleteCustomClass(AppListClass);
  147.     if(PrefsWinClass)       MUI_DeleteCustomClass(PrefsWinClass);
  148.     if(AppClass)            MUI_DeleteCustomClass(AppClass);
  149. }
  150.  
  151. /**************************************************************************/
  152.  
  153. static struct DiskObject *GetMyDiskObject(int argc, char **argv)
  154. {
  155.     STRPTR filename, iconname;
  156.     struct DiskObject *iconobj = NULL;
  157.  
  158.     if(argc)
  159.         filename = FilePart(argv[0]);
  160.     else
  161.         filename = (STRPTR)((struct WBStartup *)argv)->sm_ArgList->wa_Name;
  162.  
  163.     if((iconname = AllocVec(strlen(filename) + 9, MEMF_ANY)))
  164.     {
  165.         SPrintf(iconname, "PROGDIR:%s", filename);
  166.         iconobj = GetDiskObject(iconname);
  167.         FreeVec(iconname);
  168.     }
  169.  
  170.     return(iconobj);
  171. }
  172.  
  173. /**************************************************************************/
  174.  
  175. int main(int argc, char **argv)
  176. {
  177.     STRPTR error;
  178.     int retval = RETURN_FAIL;
  179.     struct DiskObject *dob = NULL;
  180.     ULONG sigs = 0;
  181.     Object *app = NULL, *win;
  182.  
  183.     /* open libraries */
  184.  
  185.     if(error = OpenLibs())
  186.         goto done;
  187.  
  188.     /* init global classnames */
  189.  
  190.     InitClassnames();
  191.  
  192.     /* create MUI classes */
  193.  
  194.     if(error = CreateClasses())
  195.         goto done;
  196.  
  197.     /* create MUI application object */
  198.  
  199.     app = AppObject,
  200.         MUIA_Application_Title,       APPNAME,
  201.         MUIA_Application_Version,     APPVERSTR,
  202.         MUIA_Application_Copyright,   APPCOPYRIGHT,
  203.         MUIA_Application_Author,      APPAUTHOR,
  204.         MUIA_Application_Description, APPDESCR,
  205.         MUIA_Application_Base,        APPBASENAME,
  206.         MUIA_Application_UsedClasses, UsedClasses,
  207.         MUIA_Application_DiskObject,  dob = GetMyDiskObject(argc, argv),
  208.         MUIA_Application_Window,      win = NewObject(PrefsWinClass->mcc_Class, NULL, TAG_DONE),
  209.     End;
  210.  
  211.     if(!app)
  212.     {
  213.         error = "Unable to create MUI application object";
  214.         goto done;
  215.     }
  216.  
  217.     /* get the openurl.library prefs */
  218.  
  219.     if(!DoMethod(win, MUIM_PrefsWin_GetPrefs, FALSE))
  220.         goto done;
  221.  
  222.     set(win, MUIA_Window_Open, TRUE);
  223.     if(!xget(win, MUIA_Window_Open))
  224.     {
  225.         error = "Unable to open window";
  226.         goto done;
  227.     }
  228.  
  229.     /* enter input loop */
  230.  
  231.     while(DoMethod(app, MUIM_Application_NewInput, &sigs) 
  232.           != MUIV_Application_ReturnID_Quit)
  233.     {
  234.         if(sigs)
  235.         {
  236.             sigs = Wait(sigs | SIGBREAKF_CTRL_C);
  237.             if(sigs & SIGBREAKF_CTRL_C) break;
  238.         }
  239.     }
  240.  
  241.     retval = RETURN_OK;
  242.  
  243. done:
  244.     if(error)
  245.     {
  246.         if(MUIMasterBase)
  247.             MUI_RequestA(app, NULL, 0, APPNAME " · Error", "Abort", error, NULL);
  248.         else
  249.             Printf("%s\n", error);
  250.     }
  251.  
  252.     if(app) MUI_DisposeObject(app);
  253.     DestroyClasses();
  254.     if(dob) FreeDiskObject(dob);
  255.     CloseLibs();
  256.  
  257.     return(retval);
  258. }
  259.